﻿using System;

namespace fKanban.Utility.ObjectExtend
{
    public static class ObjectExtend
    {
        public static DateTime ToDateTime(this object obj, DateTime defaultValue)
        {
            defaultValue = DateTime.MinValue;
            if (obj == null || obj is DBNull)
                return defaultValue;

            if (string.IsNullOrEmpty(obj.ToString()))
                return defaultValue;

            DateTime _value = DateTime.TryParse(obj.ToString(), out _value) ? _value : defaultValue;
            return _value;
        }

        public static int ToInt(this object obj, int defaultValue = 0)
        {
            if (obj == null || obj is DBNull)
                return defaultValue;

            if (string.IsNullOrEmpty(obj.ToString()))
                return defaultValue;

            int _value = int.TryParse(obj.ToString(), out _value) ? _value : defaultValue;
            return _value;
        }

        public static string Default(this object obj, string defaultValue = "")
        {
            if (null == obj || obj is DBNull || obj.ToString() == "0")
                return defaultValue;
            return obj.ToString();
        }
    }
}